//
//  QueryResultResidencePermitViewController.m
//  Jyxt
//
//  Created by 王炜光 on 2017/8/24.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "QueryResultResidencePermitViewController.h"
#import "ReviewResidencePermitDelegate.h"
#import "ReviewResidencePermitDataSource.h"
#import "ResidencePermitViewModel.h"
#import <MJRefresh.h>
#import "SearchResidencePermitViewModel.h"
#import "UserCenterViewModel.h"
#import <LemonBubble.h>
@interface QueryResultResidencePermitViewController ()

@property (nonatomic, strong) UITableView *queryListTableView;

@property (nonatomic , strong) ReviewResidencePermitDelegate *reviewResidencePermitDelegate;
@property (nonatomic , strong) ReviewResidencePermitDataSource *reviewResidencePermitDataSource;

@property (nonatomic , assign) NSInteger pageInt;

@property (nonatomic, strong)NSMutableArray *listArr;

@end

@implementation QueryResultResidencePermitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initView{
    
    _listArr = [[NSMutableArray alloc]init];
    _queryListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStylePlain];
    _queryListTableView.delegate = self.reviewResidencePermitDelegate;
    _queryListTableView.dataSource = self.reviewResidencePermitDataSource;
    _queryListTableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_queryListTableView];
    
    //默认block方法：设置下拉刷新
    _queryListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageInt=1;
        [_listArr removeAllObjects];
        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_pageInt] Status:@"1"];
    }];

}

- (ReviewResidencePermitDataSource *)reviewResidencePermitDataSource{
    if (!_reviewResidencePermitDataSource) {
        _reviewResidencePermitDataSource = [ReviewResidencePermitDataSource new];
    }
    _reviewResidencePermitDataSource.dataArr = _listArr;
    return _reviewResidencePermitDataSource;
}

- (ReviewResidencePermitDelegate *)reviewResidencePermitDelegate{
    if (!_reviewResidencePermitDelegate) {
        _reviewResidencePermitDelegate = [ReviewResidencePermitDelegate new];
    }
    _reviewResidencePermitDelegate.dataArr = _listArr;
    return _reviewResidencePermitDelegate;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_queryListTableView.mj_header beginRefreshing];
}

- (void)loadDataPageNo:(NSString *)pageNo Status:(NSString *)status{
    NSString *dataStr = [ResidencePermitViewModel dictionaryToJson:@{@"pageNo":pageNo,@"pageSize":@"10",@"orderBy":@""}];
    NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithDictionary:_valueDic];
    [dataDic setObject:dataStr forKey:@"data"];
    [dataDic setObject:[UserCenterViewModel getLocalUToken] forKey:@"utoken"];

    [SearchResidencePermitViewModel searchCommitWithData:dataDic whenSuccess:^(id responseObject) {
        [_listArr addObjectsFromArray:responseObject[@"data"]];
        if ([responseObject[@"lastPage"] boolValue]) {
            _queryListTableView.mj_footer = nil;
        }else{
            _queryListTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                _pageInt++;
                [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_pageInt] Status:nil];
            }];
        }
        [_queryListTableView.mj_footer endRefreshing];
        [_queryListTableView.mj_header endRefreshing];
        [_queryListTableView reloadData];
        
    } failed:^(NSString *reason) {
        
        [self showErrorWithTitle: reason autoCloseTime: 3];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
